package com.hrms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hrms.dao.AttenManageMapper;
import com.hrms.pojo.AttenManage;
import com.hrms.pojo.SalaryInfo;
import com.hrms.dao.SalaryInfoMapper;
import com.hrms.pojo.SalaryMeritFine;
import com.hrms.service.SalaryInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 薪资表 服务实现类
 *
 * @author LK
 * @since 2021-10-19
 */
@Service
public class SalaryInfoServiceImpl  implements SalaryInfoService {

	@Autowired(required = false)
	SalaryInfoMapper salaryInfoMapper;
	@Autowired(required = false)
	AttenManageMapper attenManageMapper;

	@Override
	public HashMap<String, Object> select(SalaryInfo a) {
		HashMap<String, Object> map = new HashMap<>();
		//定义分页对象
		Page<SalaryInfo> page = new Page<>(a.getPage(), a.getRow());
		QueryWrapper<SalaryInfo> queryWrapper = new QueryWrapper<>();
		//编号查询
		queryWrapper.eq(a.getStaffId() != null, "staff_id", a.getStaffId());
		//Page<AttenManage> aList = attenManageMapper.selectByCon(page, a);
		//用户名的模糊查询
		queryWrapper.like(!StringUtils.isEmpty(a.getUserName()) && a.getUserName() != null, "user_name",
				a.getUserName());
		//查询数据库
		Page<SalaryInfo> aList =salaryInfoMapper.selectPage(page,queryWrapper);
		//当前页集合
		map.put("list",aList.getRecords());
		//总页数
		map.put("totalPage",aList.getPages());
		//总条数
		map.put("total",aList.getTotal());
		//当前页
		map.put("curPage",a.getPage());
		if(a.getPage()==1){
			//上一页
			map.put("prePage",1);

		}else{
			map.put("prePage",a.getPage()-1);

		}
		if(a.getPage()==aList.getPages()){
			//下一页
			map.put("nextPage",aList.getPages());

		}else{
			map.put("nextPage",a.getPage()+1);

		}
		//每页显示条数
		map.put("row",a.getRow());

		return map;
	}

	@Override
	public HashMap<String, Object> add(SalaryInfo a) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		//查询用户名和角色
		List<SalaryMeritFine> roles = salaryInfoMapper.selectRoleByStaffId(a);
		//写入用户名
		a.setUserName(roles.get(0).getUserName());
		//根据角色code写入基本薪资
		int baseMoney = 0;//基本工资
		if ("admin".equals(roles.get(0).getRoleName())) {
			a.setSalaryMoney("5000");
			baseMoney = 5000;
		} else if ("dong".equals(roles.get(0).getRoleName())) {
			a.setSalaryMoney("6000");
			baseMoney = 6000;
		} else if ("manager".equals(roles.get(0).getRoleName())) {
			a.setSalaryMoney("5000");
			baseMoney = 5000;
		} else if ("staff".equals(roles.get(0).getRoleName())) {
			a.setSalaryMoney("4000");
			baseMoney = 4000;
		}
		//根据考勤表中薪资
		List<AttenManage> attenManages = attenManageMapper.selectList(null);
		int merit = 500;//绩效工资
		int fine = 0;//罚款
		//提取对应员工的绩效和罚款
		for (AttenManage attenManage : attenManages) {
			if (attenManage.getStaffId().equals(a.getStaffId())) {
				if ("出差".equals(attenManage.getAttenType())) {
					merit += Integer.parseInt(attenManage.getAttenReward());
				} else if ("加班".equals(attenManage.getAttenType())) {
					merit += Integer.parseInt(attenManage.getAttenReward());
				} else if ("迟到".equals(attenManage.getAttenType())) {
					fine += Integer.parseInt(attenManage.getAttenReward());
				}else if ("旷工".equals(attenManage.getAttenType())) {
					fine += Integer.parseInt(attenManage.getAttenReward());
				}else if ("请假".equals(attenManage.getAttenType())) {
					fine += Integer.parseInt(attenManage.getAttenReward());
				}else if ("调班".equals(attenManage.getAttenType())) {
					fine += Integer.parseInt(attenManage.getAttenReward());
				}else if ("停工".equals(attenManage.getAttenType())) {
					fine += 0;
				}
			}
		}
		//设置绩效
		a.setSalaryMerit(String.valueOf(merit));
		//取出奖金
		int salaryBonus = Integer.parseInt(a.getSalaryBonus());
		//设置罚款
		a.setSalaryFine(String.valueOf(fine));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//设置综合薪资
		a.setSalaryNumber(String.valueOf(merit+fine+baseMoney+salaryBonus));
		//设置时间
		a.setSalaryDate(sdf.format(new Date()));
		System.out.println(a);
		//新增
		int num = salaryInfoMapper.insert(a);
		if(num>0){
			map.put("info","保存成功");
		}else{
			map.put("info","保存错误");
		}
		return map;
	}

	@Override
	public HashMap<String, Object> update(SalaryInfo a) {
		HashMap<String, Object> map = new HashMap<String, Object>();

		int salaryMoney =Integer.parseInt(a.getSalaryMoney());
		int salaryMerit = Integer.parseInt(a.getSalaryMerit());
		int salaryBonus = Integer.parseInt(a.getSalaryBonus());
		int salaryFine = Integer.parseInt(a.getSalaryFine());
		int salaryNumber = salaryMoney + salaryMerit + salaryBonus - salaryBonus;
		a.setSalaryNumber(String.valueOf(salaryNumber));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//设置时间
		a.setSalaryDate(sdf.format(new Date()));
		//修改
		int num = salaryInfoMapper.updateById(a);
		if(num>0){
			map.put("info","保存成功");
		}else{
			map.put("info","保存错误");
		}
		return map;
	}

	@Override
	public HashMap<String, Object> del(Integer a) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		//删除
		int num = salaryInfoMapper.deleteById(a);
		if(num>0){
			map.put("info","保存成功");
		}else{
			map.put("info","保存错误");
		}
		return map;
	}


}
